<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
      <!-- v-model="entity.DepartmentId" -->
      <a-form-model-item label="选择组织机构" prop="DepartmentId" :labelCol="{ style: 'width: 20%;float:left;' }">
        <a-tree-select
          @change="Department($event)"
          allowClear
          :treeData="DepartmentIdTreeData"
          placeholder="请选择组织机构"
          treeDefaultExpandAll
          v-model="DepartmentId"
        ></a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="选择用户" :labelCol="{ style: 'width: 20%;float:left;' }">
        <a-select showSearch :filter-option="filterOption" v-model="entity.SalesmanId">
          <a-select-option v-for="item in List" :key="item.Id" :value="item.Id">
            {{ item.RealName }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
    <a-table :columns="columns" :dataSource="data" bordered :pagination="false">
      <span slot="action" slot-scope="text, record, index">
        <template>
          <!-- <a @click="handleEdit(record.Id)">编辑</a>
          <a-divider type="vertical" /> -->
          <a v-if="hasPerm('Quot_PriceListDetails.Delete')" @click="handleDelete(index, [record.Id])">取消选择</a>
        </template>
      </span>
    </a-table>
  </a-modal>
</template>

<script>
const columns = [
  { title: '客户编码', dataIndex: 'cCusCode' },
  { title: '客户名称', dataIndex: 'cCusName' },
  { title: '电话 ', dataIndex: 'cCusPhone' },
  { title: '地址', dataIndex: 'cCusAddress' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '20%' }
]
export default {
  props: {
    parentObj: Object,
    IsFormal: { type: Boolean, required: false, default: false }
  },
  data() {
    return {
      Ids: '',
      data: [],
      columns,
      DepartmentId: '',
      List: [],
      DepartmentIdTreeData: [],
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: ''
    }
  },
  methods: {
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    //选中组织机构获取人员
    Department(e) {
      this.UserList(e)
    },
    handleDelete(index) {
      this.data.splice(index, 1)
      //console.log(this.data)
      if (this.data.length == 0) this.visible = false
    },
    UserList(e) {
      this.$http.post('/Base_Manage/Base_User/GetDataListAsyncByDeptId?DeptId=' + e).then(resJson => {
        if (resJson.Success) {
          this.List = resJson.Data
        }
      })
    },
    init() {
      //console.log(this.IsFormal)
      this.$http.post('/Base_Manage/Base_Department/GetTreeDataList', {}).then(resJson => {
        if (resJson.Success) {
          this.DepartmentIdTreeData = resJson.Data
        }
      })
      this.visible = true
      this.entity = {}
      this.List = []
      this.DepartmentId = ''
    },
    close() {
      this.visible = false
      this.data = []
      this.entity = {}
    },
    openForm2(Id, title) {
      this.init()
      //console.log(Id)
      this.title = title
      if (Id) {
        //console.log(Id)
        this.Ids = Id
        this.loading = true
        this.$http.post('/CustomerManagement/Cust_CustomerInfoPotential/GetDataListAsyncById', Id).then(resJson => {
          this.loading = false

          this.data = resJson.Data
          //console.log(this.data, resJson.Data)
        })
      }
    },
    openForm(Id, title) {
      this.init()
      //console.log(Id)
      this.title = title
      if (Id) {
        //console.log(Id)
        this.Ids = Id
        this.loading = true
        this.$http.post('/CustomerManagement/Customer/GetDataListBycCusCode', Id).then(resJson => {
          this.loading = false

          this.data = resJson.Data
          //console.log(this.data, resJson.Data)
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        //console.log(this.entity)
        var entityData = { ...this.entity }

        //console.log(this.Ids)

        entityData.CustomerId_List = this.Ids
        entityData.IsFormal = this.IsFormal
        //console.log(entityData)

        this.$http.post('/CustomerManagement/Cust_CustomerAllocation/SaveData', entityData).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
